import math


class Solution(object):
    def binaryGap(self, n):
        """
        :type n: int
        :rtype: int
        """
        next_value = n & (n - 1)
        if next_value == 0:
            return 0
        now_index = math.log2(n - next_value)
        n = next_value
        max_instance = -1
        while n > 0:
            next_value = n & (n - 1)
            next_index = math.log2(n - next_value)
            max_instance = max(max_instance, next_index - now_index)
            now_index = next_index
            n = next_value
        return int(max_instance)


data = Solution()
n = 22
print(data.binaryGap(n))
